package com.wx;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.io.File;

public class MyBatisGenerator {
    public static void main(String[] args) {
        //连接数据库
        String url = "jdbc:mysql://127.0.0.1:3306/smbmsdb";
        String username = "root";
        String password = "wxok";
        DataSourceConfig dsc = new DataSourceConfig.Builder(url, username, password).build();
        //连接数据库

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator(dsc);
        // 模块名称 SMBMS_WX_BOOT 子项目名称，你要生代码的项目名称
        String moduleName = "SMBMS_WX_BOOT";
        // 项目目录  父项目路径
        String projectPath = "D:\\CMS\\SMBMS_WX_PARENT\\";
        // 作者名称
        String author = "wangxiao";
        // 基础包路径
        String packagePath = "com.wx";
        // 需要生成的表
        String tables = "smbms_bill";
        // 代码生成后是否打开磁盘目录
        String openDir = "false";
        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig
                .Builder()
                .outputDir(projectPath + "/" + moduleName + "/src/main/java")
                .author(author)
                //.openDir("true".equals(openDir))
                .commentDate("yyyy-MM-dd HH:mm:ss")
                .fileOverride() //文件是否覆盖
                .build();

        //包配置   在基础包下，包配置
        PackageConfig packageConfig = new PackageConfig
                .Builder()
                .parent(packagePath)
//                .moduleName(moduleName)
                .controller("controller")
                .entity("entity")
                .mapper("mapper")
                .service("service")
                .serviceImpl("service.impl")
                .moduleName(null)
                .build();


        // 配置模板  根据模板失衡从
        String absolutePath = File.separator + "templates"+ File.separator;
        String mapperTempPath = absolutePath + File.separator + "Mapper";
        String entityTempPath = absolutePath + File.separator + "Entity";
        String serviceTempPath = absolutePath + File.separator + "Service";
        String serviceImplTempPath = absolutePath + File.separator + "ServiceImpl";
        String controllerPath = absolutePath + File.separator + "Controller";
        TemplateConfig templateConfig = new TemplateConfig
                .Builder()
                .mapper(mapperTempPath)
                .service(serviceTempPath)
                .serviceImpl(serviceImplTempPath)
                .entity(entityTempPath)
                .mapperXml(null)
                .controller(controllerPath)
                .build();


        // 策略配置  总，包含所有  包，模板 全局
        StrategyConfig strategyConfig = new StrategyConfig
                .Builder()
                .addInclude(tables.split(","))
                .entityBuilder()
                .naming(NamingStrategy.underline_to_camel)
                .serviceBuilder().formatServiceFileName("%sService")
//                .enableLombok()
//                .controllerBuilder()
//                .enableRestStyle()
                .build();
        mpg.global(globalConfig);
        mpg.packageInfo(packageConfig);
        mpg.template(templateConfig);
        mpg.strategy(strategyConfig);
        // 开始生成代码文件
        mpg.execute(new FreemarkerTemplateEngine());
    }
}
